package com.polaris.live.popup

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.adapter.NikeNameRandomAdapter
import com.polaris.live.common.R
import com.polaris.live.common.databinding.PopupNikenameBinding
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.interface_bag.TextWatcherAdapter
import com.polaris.live.translation.R.string
import com.polaris.live.viewmodel.InfoUserViewModel

/**
 * 昵称弹窗
 *
 * @author Created by 长空 on 2023/10/25/004 17:21
 */
class NikeNamePopup(
    context: Context,
    private val mViewModel: InfoUserViewModel,
    private val mMyName: String,
    private val mOnNikeNameCallBack: ((nikeName: String) -> Unit),
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupNikenameBinding
    private val mAdapter by lazy { NikeNameRandomAdapter() }

    private var mNikeName = ""

    override fun getImplLayoutId(): Int {
        return R.layout.popup_nikename
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupNikenameBinding.bind(popupImplView)

        mBinding.recyclerView.adapter = mAdapter

        if (!TextUtils.isEmpty(mMyName)) {
            mBinding.etName.setText(mMyName)
            mBinding.tvNum.text = "${mMyName.length}/20"
            mBinding.etName.text?.length?.let { mBinding.etName.setSelection(it) }
        }

        initObserver()
        initListener()
    }

    private fun initListener() {
        mBinding.etName.addTextChangedListener(object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                // 在文本改变之后的回调
                val userInput = s.toString()
                // 检查是否以空格开头或结尾
                if (userInput.startsWith(" ") || userInput.endsWith(" ")) {
                    // 如果以空格开头或结尾，移除空格
                    val trimmedText = userInput.trim { it <= ' ' }
                    mBinding.etName.setText(trimmedText)
                    mBinding.etName.setSelection(trimmedText.length) // 将光标移至末尾
                }
                mBinding.spSave.isEnabled = (s?.length ?: 0) > 1
                mBinding.tvNum.text = "${s?.length}/20"
                mNikeName = s.toString()
            }
        })

        mBinding.icBack.setOnSingleClickListener {
            dismiss()
        }

        mBinding.spSave.setOnSingleClickListener {
            if (TextUtils.isEmpty(mBinding.etName.text.toString().trim())) {
                ToastGravity.showGravity(StringUtils.getString(string.please_input_name))
                return@setOnSingleClickListener
            }
            mViewModel.infoUpNameData(mNikeName)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            if ((mAdapter.data[position].nickname?.length ?: 0) > 20) {
                return@setOnItemClickListener
            }
            mBinding.ivProhibit.visibility = View.GONE
            mBinding.tvNum.visibility = View.VISIBLE
            mBinding.etName.shapeDrawableBuilder.setStrokeColor(ColorUtils.getColor(com.polaris.live.R.color.color_transparent))
                .intoBackground()
            mNikeName = mAdapter.data[position].nickname.toString()
            mBinding.etName.setText(mAdapter.data[position].nickname.toString())
            mAdapter.data[position].nickname?.length?.let { mBinding.etName.setSelection(it) }
        }
    }

    fun initObserver() {
        mViewModel.mInfoUpNameLiveData.observe(this) {
            mOnNikeNameCallBack.invoke(mNikeName)
            dismiss()
        }
        mViewModel.mRandomNameLiveData.observe(this) { randomName ->
            mBinding.ivProhibit.visibility = View.VISIBLE
            mBinding.tvNum.visibility = View.GONE
            mBinding.etName.shapeDrawableBuilder.setStrokeColor(ColorUtils.getColor(R.color.color_FE4F3C))
                .intoBackground()
            mAdapter.setList(randomName)
        }
    }
}